package com.epfly.promo.activity.impl.discount;

import com.epfly.iface.promo.activity.enums.DiscountTypeEnum;
import com.epfly.promo.activity.entity.ActivityRuleEntity;
import com.epfly.promo.activity.impl.discount.content.OrderFull;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Comparator;

/**
 * 1.满减;
 * {@link DiscountTypeEnum#ORDER_FULL}
 */
@Service
public class OrderFullDiscountFunction extends AbstractDiscountFunction {



    @Override
    public BigDecimal doApply(ActivityWrapper activityWrapper, ActivityRuleEntity ruleEntity) {
        BigDecimal payAmount = activityWrapper.getPayAmount();
        String ruleContent = ruleEntity.getRuleContent();
        OrderFull orderFull = gson.fromJson(ruleContent, OrderFull.class);

        BigDecimal totalRebate = orderFull.getValues().stream().map(e -> {
            if (payAmount.compareTo(e.getUpperBound()) >= 0) {
                return e.getDiscountAmount();
            }
            return BigDecimal.ZERO;
        }).max(Comparator.naturalOrder()).get();

        return totalRebate;
    }

    @Override
    public Byte getDiscountType() {
        return DiscountTypeEnum.ORDER_FULL.getKey();
    }
}
